﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class PaintingPanel : MonoBehaviour
{
    //定义变量
    private List<InputField> listInputField = new List<InputField>();//用来管理InputFild的容器
    private RawImage RawImage;
    private Image SignImage;
    private GameObject Window;
    private Image ColorTogView;
    private Image Viewport;
    private GameObject Content;
    private Toggle ColorTog1;
    private Image Background;
    private Image Checkmark;
    private Toggle ColorTog2;
    private Image Background1;
    private Image Checkmark2;
    private Toggle ColorTog3;
    private Image Background3;
    private Image Checkmark4;
    private Toggle ColorTog4;
    private Image Background5;
    private Image Checkmark6;
    private Toggle ColorTog5;
    private Image Background7;
    private Image Checkmark8;
    private Toggle ColorTog6;
    private Image Background9;
    private Image Checkmark10;
    private Toggle ColorTog7;
    private Image Background11;
    private Image Checkmark12;
    private Image PenTogView;
    private GameObject PenPoint;
    private Toggle PenTog1;
    private Image Background13;
    private Image Checkmark14;
    private Image PenColor;
    private GameObject PenPoint2;
    private Toggle PenTog2;
    private Image Background15;
    private Image Checkmark16;
    private Image PenColor17;
    private GameObject PenPoint3;
    private Toggle PenTog3;
    private Image Background18;
    private Image Checkmark19;
    private Image PenColor20;
    private GameObject PenPoint4;
    private Toggle PenTog4;
    private Image Background21;
    private Image Background122;
    private Image Checkmark23;
    private Button ExitBtn;
    private Button ClearBtn;

    private Painting painting;

    private PenTog penTog;
    GameObject SizeChangePanel;
    float sizeTemp;

    private void Awake()
    {
        painting = GetComponent<Painting>();
        InitFunc();      //获取组件
        AddLisiners();//监听事件
        penTog = PenTog1.GetComponent<PenTog>();//默认为第一支笔
    }

    /// <summary>
    /// 讲所有颜色按钮设置为不可选取
    /// </summary>
    /// <param name="b"></param>
    private void SetActiveColorTog(bool b)
    {
        ColorTog1.interactable = b;
        ColorTog2.interactable = b;
        ColorTog3.interactable = b;
        ColorTog4.interactable = b;
        ColorTog5.interactable = b;
        ColorTog6.interactable = b;
        ColorTog7.interactable = b;
    }

    /// <summary>
    /// 更换笔刷
    /// </summary>
    /// <param name="texture"></param>
    private void ChangeBrush(Texture texture)
    {
        if (texture)
        {
            painting.brushTypeTexture = texture;
            Debug.Log("成功切换笔刷和颜色");
        }
        else
        {
            Debug.Log("笔刷材质为空");
        }
    }

    /// <summary>
    /// 清理颜色的Tog
    /// </summary>
    private void ClearColorTog()
    {
        ColorTog1.isOn = false;
        ColorTog2.isOn = false;
        ColorTog3.isOn = false;
        ColorTog4.isOn = false;
        ColorTog5.isOn = false;
        ColorTog6.isOn = false;
        ColorTog7.isOn = false;
    }
    /// <summary>
    /// 换笔刷的颜色
    /// </summary>
    /// <param name="color"></param>
    private void ChangeColor(Color color)
    {
        if (penTog)
        {
            penTog.SetThisBrushColor(color);
            painting.brushColor = penTog.colorImage.color;
        }
    }

    //初始化函数 用于获取UI组件等
    private void InitFunc()
    {
        RawImage = ToolUnity.FindGameObject(gameObject, "RawImage").GetComponent<RawImage>();
        SignImage = ToolUnity.FindGameObject(gameObject, "SignImage").GetComponent<Image>();
        Window = ToolUnity.FindGameObject(gameObject, "Window").GetComponent<GameObject>();
        ColorTogView = ToolUnity.FindGameObject(gameObject, "ColorTogView").GetComponent<Image>();
        Viewport = ToolUnity.FindGameObject(gameObject, "Viewport").GetComponent<Image>();
        Content = ToolUnity.FindGameObject(gameObject, "Content").GetComponent<GameObject>();
        ColorTog1 = ToolUnity.FindGameObject(gameObject, "ColorTog1").GetComponent<Toggle>();
        Background = ToolUnity.FindGameObject(gameObject, "Background").GetComponent<Image>();
        Checkmark = ToolUnity.FindGameObject(gameObject, "Checkmark").GetComponent<Image>();
        ColorTog2 = ToolUnity.FindGameObject(gameObject, "ColorTog2").GetComponent<Toggle>();
        Background1 = ToolUnity.FindGameObject(gameObject, "Background1").GetComponent<Image>();
        Checkmark2 = ToolUnity.FindGameObject(gameObject, "Checkmark2").GetComponent<Image>();
        ColorTog3 = ToolUnity.FindGameObject(gameObject, "ColorTog3").GetComponent<Toggle>();
        Background3 = ToolUnity.FindGameObject(gameObject, "Background3").GetComponent<Image>();
        Checkmark4 = ToolUnity.FindGameObject(gameObject, "Checkmark4").GetComponent<Image>();
        ColorTog4 = ToolUnity.FindGameObject(gameObject, "ColorTog4").GetComponent<Toggle>();
        Background5 = ToolUnity.FindGameObject(gameObject, "Background5").GetComponent<Image>();
        Checkmark6 = ToolUnity.FindGameObject(gameObject, "Checkmark6").GetComponent<Image>();
        ColorTog5 = ToolUnity.FindGameObject(gameObject, "ColorTog5").GetComponent<Toggle>();
        Background7 = ToolUnity.FindGameObject(gameObject, "Background7").GetComponent<Image>();
        Checkmark8 = ToolUnity.FindGameObject(gameObject, "Checkmark8").GetComponent<Image>();
        ColorTog6 = ToolUnity.FindGameObject(gameObject, "ColorTog6").GetComponent<Toggle>();
        Background9 = ToolUnity.FindGameObject(gameObject, "Background9").GetComponent<Image>();
        Checkmark10 = ToolUnity.FindGameObject(gameObject, "Checkmark10").GetComponent<Image>();
        ColorTog7 = ToolUnity.FindGameObject(gameObject, "ColorTog7").GetComponent<Toggle>();
        Background11 = ToolUnity.FindGameObject(gameObject, "Background11").GetComponent<Image>();
        Checkmark12 = ToolUnity.FindGameObject(gameObject, "Checkmark12").GetComponent<Image>();
        PenTogView = ToolUnity.FindGameObject(gameObject, "PenTogView").GetComponent<Image>();
        PenPoint = ToolUnity.FindGameObject(gameObject, "PenPoint").GetComponent<GameObject>();
        PenTog1 = ToolUnity.FindGameObject(gameObject, "PenTog1").GetComponent<Toggle>();
        Background13 = ToolUnity.FindGameObject(gameObject, "Background13").GetComponent<Image>();
        Checkmark14 = ToolUnity.FindGameObject(gameObject, "Checkmark14").GetComponent<Image>();
        PenColor = ToolUnity.FindGameObject(gameObject, "PenColor").GetComponent<Image>();
        PenPoint2 = ToolUnity.FindGameObject(gameObject, "PenPoint2").GetComponent<GameObject>();
        PenTog2 = ToolUnity.FindGameObject(gameObject, "PenTog2").GetComponent<Toggle>();
        Background15 = ToolUnity.FindGameObject(gameObject, "Background15").GetComponent<Image>();
        Checkmark16 = ToolUnity.FindGameObject(gameObject, "Checkmark16").GetComponent<Image>();
        PenPoint3 = ToolUnity.FindGameObject(gameObject, "PenPoint3").GetComponent<GameObject>();
        PenTog3 = ToolUnity.FindGameObject(gameObject, "PenTog3").GetComponent<Toggle>();
        Background18 = ToolUnity.FindGameObject(gameObject, "Background18").GetComponent<Image>();
        Checkmark19 = ToolUnity.FindGameObject(gameObject, "Checkmark19").GetComponent<Image>();
        PenPoint4 = ToolUnity.FindGameObject(gameObject, "PenPoint4").GetComponent<GameObject>();
        PenTog4 = ToolUnity.FindGameObject(gameObject, "PenTog4").GetComponent<Toggle>();
        Background21 = ToolUnity.FindGameObject(gameObject, "Background21").GetComponent<Image>();
        Background122 = ToolUnity.FindGameObject(gameObject, "Background122").GetComponent<Image>();
        Checkmark23 = ToolUnity.FindGameObject(gameObject, "Checkmark23").GetComponent<Image>();
        ExitBtn = ToolUnity.FindGameObject(gameObject, "ExitBtn").GetComponent<Button>();
        ClearBtn = ToolUnity.FindGameObject(gameObject, "ClearBtn").GetComponent<Button>();
    }

    //将模型层的数据同步到View层
    private void SetData()
    {

    }
    bool canSizePanelTrue = false;
    //初始化函数 用于获取UI组件等
    private void AddLisiners()
    {
        ColorTog1.onValueChanged.AddListener((e) => {
            Debug.Log("ColorTog1 (UnityEngine.GameObject):" + e);
            if (e)
            {
                ChangeColor(ColorTog1.GetComponent<ColorTog>().togColor);
            }
        });

        ColorTog2.onValueChanged.AddListener((e) => {
            Debug.Log("ColorTog2 (UnityEngine.GameObject):" + e);
            if (e)
            {
                ChangeColor(ColorTog2.GetComponent<ColorTog>().togColor);
            }
        });

        ColorTog3.onValueChanged.AddListener((e) => {
            Debug.Log("ColorTog3 (UnityEngine.GameObject):" + e);
            if (e)
            {
                ChangeColor(ColorTog3.GetComponent<ColorTog>().togColor);
            }
        });

        ColorTog4.onValueChanged.AddListener((e) => {
            Debug.Log("ColorTog4 (UnityEngine.GameObject):" + e);
            if (e)
            {
                ChangeColor(ColorTog4.GetComponent<ColorTog>().togColor);
            }
        });

        ColorTog5.onValueChanged.AddListener((e) => {
            Debug.Log("ColorTog5 (UnityEngine.GameObject):" + e);
            if (e)
            {
                ChangeColor(ColorTog5.GetComponent<ColorTog>().togColor);
            }
        });

        ColorTog6.onValueChanged.AddListener((e) => {
            Debug.Log("ColorTog6 (UnityEngine.GameObject):" + e);
            if (e)
            {
                ChangeColor(ColorTog6.GetComponent<ColorTog>().togColor);
            }
        });

        ColorTog7.onValueChanged.AddListener((e) => {
            Debug.Log("ColorTog7 (UnityEngine.GameObject):" + e);
            if (e)
            {
                ChangeColor(ColorTog7.GetComponent<ColorTog>().togColor);
            }
        });



        PenTog1.onValueChanged.AddListener((e) => {
            if (e)
            {
                PenTog1.transform.DOLocalMove(new Vector3(0,-20,0),0.3f);
                if (canSizePanelTrue)
                {
                    GameObject obj = AssetsPool.Instance.LoadGameObject("SizeChangePanel","UI/Paint/");
                    SizeChangePanel = Instantiate(obj,transform);
                    canSizePanelTrue = false;
                }
                else
                {
                    Destroy(SizeChangePanel);
                    canSizePanelTrue = true;
                }
                ClearColorTog();
                penTog = PenTog1.GetComponent<PenTog>();
                ChangeBrush(penTog.penTexture);
                painting.brushColor = penTog.colorImage.color;
                canSizePanelTrue = true;
            }
            else
            {
                canSizePanelTrue = false;
                PenTog1.transform.DOLocalMove(new Vector3(0, -100, 0), 0.3f);
            }
        });

        PenTog2.onValueChanged.AddListener((e) => {
            if (e)
            {
                PenTog2.transform.DOLocalMove(new Vector3(0, -20, 0), 0.3f);
                if (canSizePanelTrue)
                {
                    GameObject obj = AssetsPool.Instance.LoadGameObject("SizeChangePanel", "UI/Paint/");
                    SizeChangePanel = Instantiate(obj, transform);
                    canSizePanelTrue = false;
                }
                else
                {
                    Destroy(SizeChangePanel);
                    canSizePanelTrue = true;
                }
                ClearColorTog();
                penTog = PenTog2.GetComponent<PenTog>();
                ChangeBrush(penTog.penTexture);
                painting.brushColor = penTog.colorImage.color;
            }
            else
            {
                canSizePanelTrue = false;
                PenTog2.transform.DOLocalMove(new Vector3(0, -100, 0), 0.3f);
            }
        });
        PenTog3.onValueChanged.AddListener((e) => {
            if (e)
            {
                PenTog3.transform.DOLocalMove(new Vector3(0, -20, 0), 0.3f);
                if (canSizePanelTrue)
                {
                    GameObject obj = AssetsPool.Instance.LoadGameObject("SizeChangePanel", "UI/Paint/");
                    SizeChangePanel = Instantiate(obj, transform);
                    canSizePanelTrue = false;
                }
                else
                {
                    Destroy(SizeChangePanel);
                    canSizePanelTrue = true;
                }
                ClearColorTog();
                penTog = PenTog3.GetComponent<PenTog>();
                ChangeBrush(penTog.penTexture);
                painting.brushColor = penTog.colorImage.color;
            }
            else
            {
                canSizePanelTrue = false;
                PenTog3.transform.DOLocalMove(new Vector3(0,-100,0),0.3f);
            }
        });
        PenTog4.onValueChanged.AddListener((e) => {
            if (e)
            {
                SetActiveColorTog(false);
                PenTog4.transform.DOLocalMove(new Vector3(0, -20, 0), 0.3f);
                if (canSizePanelTrue)
                {
                    GameObject obj = AssetsPool.Instance.LoadGameObject("SizeChangePanel", "UI/Paint/");
                    SizeChangePanel = Instantiate(obj, transform);
                    canSizePanelTrue = false;
                }
                else
                {
                    Destroy(SizeChangePanel);
                    canSizePanelTrue = true;
                }
                ClearColorTog();
                penTog = PenTog4.GetComponent<PenTog>();
                ChangeBrush(penTog.penTexture);
                painting.brushColor = penTog.colorImage.color;
                sizeTemp = painting.size;//临时记入之前的数据
                painting.size = 8;
            }
            else
            {
                canSizePanelTrue = false;
                painting.size = sizeTemp;//设回原来的数据
                SetActiveColorTog(true);
                PenTog4.transform.DOLocalMove(new Vector3(0, -100, 0), 0.3f);
            }
        });

        ExitBtn.onClick.AddListener(() => {
            Debug.Log("ExitBtn (UnityEngine.GameObject)");
            Application.Quit();
        });
        ClearBtn.onClick.AddListener(() => {
            Debug.Log("ClearBtn (UnityEngine.GameObject)");
            painting.OnClickClear();
        });
        if (listInputField.Count > 0)
        {
            foreach (InputField item in listInputField)
            {
                item.onValueChanged.AddListener(value => { Debug.Log(item.name + ":" + value); });
            }
        }
    }
}
